import 'package:baselib/utils/image_util.dart';
import 'package:baselib/utils/px_base_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:flutter/material.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/comm_password_form_field_widget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class PointPasswordInputDialog extends Dialog {
  String? negativeText;
  String? positiveText;
  TextStyle? negativeTextStyle;
  TextStyle? positiveTextStyle;
  Function? onCloseEvent;
  Function? onPositivePressEvent;
  bool? isTouchOutSideDissmiss;
  bool? isTouchKeyBackDissmiss;


  PointPasswordInputDialog({
    super.key,
    this.negativeTextStyle,
    this.positiveTextStyle,
    this.negativeText,
    this.positiveText,
    this.onPositivePressEvent,
    this.isTouchOutSideDissmiss=true,
    this.isTouchKeyBackDissmiss=true,
    this.onCloseEvent,
  });

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: (){
        if(isTouchKeyBackDissmiss??false){
          Navigator.of(context).pop();
        }
        return Future(() => false);
      },
      child: GestureDetector(
        onTap: (){
          if(isTouchOutSideDissmiss??false){
            Navigator.of(context).pop();
          }
        },
        child: Material(
          type: MaterialType.transparency,
          child: PointPasswordInputWidget(
            negativeText: negativeText,
            negativeTextStyle: negativeTextStyle,
            positiveText: positiveText,
            positiveTextStyle: positiveTextStyle,
            onPositivePressEvent: onPositivePressEvent,
            onCloseEvent: onCloseEvent,
          ),
        ),
      ),
    );
  }

}

class PointPasswordInputWidget extends StatefulWidget {
  String? negativeText;
  String? positiveText;
  TextStyle? negativeTextStyle;
  TextStyle? positiveTextStyle;
  Function? onCloseEvent;
  Function? onPositivePressEvent;
  PointPasswordInputWidget({
    super.key,
    this.negativeTextStyle,
    this.positiveTextStyle,
    this.negativeText,
    this.positiveText,
    this.onPositivePressEvent,
    this.onCloseEvent,
  });

  @override
  State<PointPasswordInputWidget> createState() => _PointPasswordInputWidgetState();
}

class _PointPasswordInputWidgetState extends State<PointPasswordInputWidget> {

  String passwordCur = '';

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        GestureDetector(
          onTap: (){

          },
          child:new Container(
            decoration: ShapeDecoration(
              color: color_white,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(px_10),
                ),
              ),
            ),
            constraints: BoxConstraints(
              // minHeight: ScreenUtil.getInstance().getAdapterSize(PxUtils.px_65),
                maxWidth: MediaQuery.sizeOf(context).width*3/4
            ),
            margin:  EdgeInsets.only(
                bottom: pxStatusBarHeight(context: context)
            ),
            child:
            new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                // Container(
                //   padding: EdgeInsets.only(
                //     left: px_15.w,
                //     top: px_21.h,
                //     bottom: px_12.h,
                //   ),
                //   child: Text(
                //     string_login_password,
                //     style: TextStyle(
                //       fontSize: px_18.sp,
                //       fontWeight: FontWeight.w500
                //     ),
                //   ),
                // ),
                Container(
                  margin: EdgeInsets.only(
                    left: px_15.w,
                    right: px_15.w,
                    top: px_26.h,
                    bottom: px_21.h,
                  ),
                  child: CommPasswordFormFieldWidget(
                    defaultCont: '',
                    labelText: string_login_password,
                    hintText: string_please_enter_login_password,
                    isShowPrefixIcon: false,
                    contentPadding: EdgeInsets.only(
                      left: px_10.h,
                    ),
                    onChanged: (value)=>(passwordCur=value.trim()),
                  ),
                ),
                Container(
                  color:  Color(0xffe0e0e0),
                  height: 0.5,
                ),
                Container(
                  child: _buildBottomButtonGroup(context),
                )
              ],
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildBottomButtonGroup(BuildContext context) {
    List<Widget> widgets = <Widget>[];
    if ((widget.negativeText??'').isNotEmpty) widgets.add(_buildBottomCancelButton(context));
    widgets.add(new Container(
      color: Color(0xffe0e0e0),
      width: 0.5,
      height: 20,
    ));
    if (widget.positiveText != null && widget.positiveText!.isNotEmpty) widgets.add(_buildBottomPositiveButton(context));
    return new Flex(
      direction: Axis.horizontal,
      children: widgets,
    );
  }

  Widget _buildBottomCancelButton(BuildContext context) {
    return Flexible(
      fit: FlexFit.tight,
      child: GestureDetector(
        onTap: widget.onCloseEvent==null?(){
          Navigator.of(context).pop();
        }:(()=>widget.onCloseEvent!()),
        child: Container(
          color: Colors.transparent,
          width: double.infinity,
          padding: EdgeInsets.only(
            top: px_12,
            bottom: px_12,
          ),
          alignment: Alignment.center,
          child: new Text(
            widget.negativeText??'',
            textAlign: TextAlign.center,
            style: widget.negativeTextStyle??TextStyle(
                fontSize: px_14,
                color: Color(0xff888888)
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildBottomPositiveButton(BuildContext context) {
    return new Flexible(
      fit: FlexFit.tight,
      child: GestureDetector(
        onTap: (){
          if(passwordCur.trim().isEmpty){
            ToastUtil.toast(string_please_enter_login_password);
            return;
          }
          Navigator.of(context).pop();
          if(widget.onPositivePressEvent!=null)widget.onPositivePressEvent!(passwordCur);
        },
        child: Container(
          color: Colors.transparent,
          width: double.infinity,
          padding: EdgeInsets.only(
            top: px_12,
            bottom: px_12,
          ),
          alignment: Alignment.center,
          child: new Text(
            widget.positiveText??'',
            style: widget.positiveTextStyle??TextStyle(
              color: Colors.blue,
              fontSize: px_14,
            ),
          ),
        ),
      ),
    );
  }
}
